//
//  TF_GoodsListView.swift
//  TalkfunSDKDemo
//
//  Created by 莫瑞权 on 2022/3/27.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

class TF_GoodsListView: TF_TopRightOrCornerTopLeft, UITableViewDelegate, UITableViewDataSource {

    
    @objc public let list = NSMutableArray.init()
    @objc public let title = UILabel()
    let lineView = UIView()
    
    @objc public let tableView = UITableView()
    var isShowView:Bool = false
    
    override init(frame: CGRect) {
        super.init(frame: frame)

        self.backgroundColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0)
     
  
        self.title.numberOfLines = 0;
        self.addSubview(self.title)
        self.title.textAlignment = .center;
        self.title.text = "商品（1个）";
        self.title.font = UIFont.PingFang(size: 16)
  
        self.title.textColor = UIColor(red: 35/255.0, green: 57/255.0, blue: 64/255.0, alpha: 1.0)
        
 

        //分割线

        self.addSubview(self.lineView)
        self.lineView.backgroundColor = UIColor(red: 235/255.0, green: 239/255.0, blue: 242/255, alpha: 1.0)
     

        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.addSubview(self.tableView)
        self.tableView.separatorStyle = .none;
        
        
        self.tableView.register(TF_GoodsListViewCell.self, forCellReuseIdentifier: "TF_GoodsListViewCell")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        self.title.frame = CGRect(x: 0, y: 0, width: self.frame.size.width, height: 44)
        self.lineView.frame = CGRect(x: 0, y: self.title.frame.maxY, width: self.frame.size.width, height: 0.5)
        let tableViewY:CGFloat = self.lineView.frame.maxY + 10
        self.tableView.frame = CGRect(x:0 , y: tableViewY, width: self.frame.size.width , height: self.frame.size.height - tableViewY)
        

        
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.list.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TF_GoodsListViewCell", for: indexPath) as! TF_GoodsListViewCell
        cell.selectionStyle = .none
        let model:TFgoods = self.list[indexPath.row] as! TFgoods
        cell.cellWidth = self.frame.size.width;
        
        var cellHeight:CGFloat = 115
        
        let qrcode:NSString =  model.qrcode as NSString
        if (qrcode.length > 0 ) {
            cellHeight = cellHeight + 22.5
        }
        cell.cellHeight = cellHeight ;
        cell.model = model;
        
        return cell
    }
    // 设置cell高度
       func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
           let model:TFgoods = self.list[indexPath.row] as! TFgoods
         
           var cellHeight:CGFloat = 115
           
           let qrcode:NSString =  model.qrcode as NSString
           if (qrcode.length > 0 ) {
               cellHeight = cellHeight + 22.5
           }
           
           return cellHeight
       }
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        
        if (self.isUserInteractionEnabled == false || self.isHidden == true || self.alpha <= 0.01) {
            return nil
        }
     
        if (self.point(inside: point, with: event ) ) {
            // 3.从后往前遍历自己的子控件
            let count = self.subviews.count as Int

            for i in 0..<count {
                // 取出子控件
               let childV = self.subviews[i] as UIView
                // // 把自己坐标系上点转化成子控件坐标系上点
               
                let childP = self.convert(point, to: childV)
              
                let fitView = childV.hitTest(childP, with: event)
                if (fitView != nil) {
                    return fitView
                }
            }
        }
        if (self.isShowView) {
            self.showView(show: false)
          
        }
        return nil;
    }
    
    func showView(show:Bool)
    {
        self.isShowView = show
        UIView.animate(withDuration: 0.5) {
            if((self.superview) != nil)
            {
                if (show) {
                    self.frame = CGRect(x: 0, y: self.superview!.frame.size.height - 346, width: self.frame.size.width, height: 346 + 44)
                }else{
                    self.frame = CGRect(x: 0, y: self.superview!.frame.size.height, width: self.frame.size.width, height: 346 + 44)
                    
                }
            }
            
        }
       
    }
   
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
